﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class Member
    {
        private readonly KeRui.DAL.Member dal = new KeRui.DAL.Member();

        public int Add(KeRui.Model.Member model, string Password)
        {
            return this.dal.Add(model, Password);
        }

        public int AddReturnUserID(KeRui.Model.Member model, string Password)
        {
            return this.dal.AddReturnUserID(model, Password);
        }

        public int ChageState(int UserID, int isAdmin)
        {
            return this.dal.ChageState(UserID, isAdmin);
        }

        public bool CheckEmailExist(string userName)
        {
            return this.dal.CheckEmailExist(userName);
        }

        public List<KeRui.Model.Member> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.Member> list = new List<KeRui.Model.Member>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.Member item = new KeRui.Model.Member();
                    if (dt.Rows[i]["UserID"].ToString() != "")
                    {
                        item.UserID = int.Parse(dt.Rows[i]["UserID"].ToString());
                    }
                    item.Username = dt.Rows[i]["Username"].ToString();
                    if (dt.Rows[i]["GroupID"].ToString() != "")
                    {
                        item.GroupID = int.Parse(dt.Rows[i]["GroupID"].ToString());
                    }
                    item.GroupName = dt.Rows[i]["GroupName"].ToString();
                    item.Email = dt.Rows[i]["Email"].ToString();
                    if (dt.Rows[i]["EmailState"].ToString() != "")
                    {
                        item.EmailState = int.Parse(dt.Rows[i]["EmailState"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    item.Question = dt.Rows[i]["Question"].ToString();
                    item.Answer = dt.Rows[i]["Answer"].ToString();
                    if (dt.Rows[i]["LoginTimes"].ToString() != "")
                    {
                        item.LoginTimes = int.Parse(dt.Rows[i]["LoginTimes"].ToString());
                    }
                    if (dt.Rows[i]["LastLoginTime"].ToString() != "")
                    {
                        item.LastLoginTime = DateTime.Parse(dt.Rows[i]["LastLoginTime"].ToString());
                    }
                    item.LastLoginIP = dt.Rows[i]["LastLoginIP"].ToString();
                    if (dt.Rows[i]["ThisLoginTime"].ToString() != "")
                    {
                        item.ThisLoginTime = DateTime.Parse(dt.Rows[i]["ThisLoginTime"].ToString());
                    }
                    item.ThisLoginIP = dt.Rows[i]["ThisLoginIP"].ToString();
                    if (dt.Rows[i]["ActiveTime"].ToString() != "")
                    {
                        item.ActiveTime = DateTime.Parse(dt.Rows[i]["ActiveTime"].ToString());
                    }
                    if (dt.Rows[i]["State"].ToString() != "")
                    {
                        item.State = int.Parse(dt.Rows[i]["State"].ToString());
                    }
                    if (dt.Rows[i]["IsVIP"].ToString() != "")
                    {
                        item.IsVIP = int.Parse(dt.Rows[i]["IsVIP"].ToString());
                    }
                    if (dt.Rows[i]["VIPStartTime"].ToString() != "")
                    {
                        item.VIPStartTime = DateTime.Parse(dt.Rows[i]["VIPStartTime"].ToString());
                    }
                    if (dt.Rows[i]["VIPEndTime"].ToString() != "")
                    {
                        item.VIPEndTime = DateTime.Parse(dt.Rows[i]["VIPEndTime"].ToString());
                    }
                    if (dt.Rows[i]["Birthday"].ToString() != "")
                    {
                        item.Birthday = DateTime.Parse(dt.Rows[i]["Birthday"].ToString());
                    }
                    item.RealName = dt.Rows[i]["RealName"].ToString();
                    item.Sign = dt.Rows[i]["Sign"].ToString();
                    item.QQ = dt.Rows[i]["QQ"].ToString();
                    item.MSN = dt.Rows[i]["MSN"].ToString();
                    item.Guid = dt.Rows[i]["Guid"].ToString();
                    if (dt.Rows[i]["Integral"].ToString() != "")
                    {
                        item.Integral = int.Parse(dt.Rows[i]["Integral"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int UserID)
        {
            this.dal.Delete(UserID);
        }

        public int GetCount(string strWhere)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetCount("Member", strWhere);
        }

        public List<KeRui.Model.Member> GetList(int isAdmin)
        {
            string strWhere = "";
            if (isAdmin == 1)
            {
                strWhere = strWhere + " UserID in (SELECT UserID FROM Admin) ";
            }
            else if (isAdmin == -1)
            {
                strWhere = strWhere + " UserID not in (SELECT UserID FROM Admin) ";
            }
            DataSet set = new KeRui.DAL.Common().GetList("Member", -1, -1, strWhere, "UserID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.Member> GetList(int PageSize, int PageIndex, string strWhere)
        {
            DataSet set = new KeRui.DAL.Common().GetList("Member", PageSize, PageIndex, strWhere, "UserID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public KeRui.Model.Member GetModel(int UserID)
        {
            return this.dal.GetModel(UserID);
        }

        public KeRui.Model.Member GetModelByCache(int UserID)
        {
            string cacheKey = "MemberModel-" + UserID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(UserID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.Member) cache;
        }

        public DataSet GetSel()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("MemberGroup", "", " GroupID ", " GroupID,GroupName ");
        }

        public int Update(KeRui.Model.Member model)
        {
            return this.dal.Update(model);
        }

        public int Update_AllInfo(KeRui.Model.Member model)
        {
            return this.dal.Update_AllInfo(model);
        }

        public int UpdateBasicInfo(KeRui.Model.Member model)
        {
            return this.dal.UpdateBasicInfo(model);
        }

        public int UpdatePwd(string name, string question, string answer, string newpwd)
        {
            return this.dal.UpdatePwd(name, question, answer, newpwd);
        }
    }
}

